package util.hadoop;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

public class FlowWritable implements WritableComparable<FlowWritable> {

	private Text src;
	private Text dst;
	private Text pro;
	private Text srcPort;
	private Text dstPort;
	
	public FlowWritable() {
		set(new Text(),new Text(),new Text(),new Text(),new Text());
	}
	
	public FlowWritable(Text src, Text dst, Text pro, Text srcPort, Text dstPort) {
		set(src, dst, pro, srcPort, dstPort);
	}
	
	public void set(Text src, Text dst, Text pro, Text srcPort, Text dstPort) {
		this.src = src;
		this.dst = dst;
		this.pro = pro;
		this.srcPort = srcPort;
		this.dstPort = dstPort;
	}
	
	public Text getSrc() {
		return src;
	}
	
	public Text getDst() {
		return dst;
	}
	
	public Text getPro() {
		return pro;
	}
	
	public Text getSrcPort() {
		return srcPort;
	}
	
	public Text getDstPort() {
		return dstPort;
	}
	
	@Override
	public void readFields(DataInput arg0) throws IOException {
		// TODO Auto-generated method stub
		src.readFields(arg0);
		dst.readFields(arg0);
		pro.readFields(arg0);
		srcPort.readFields(arg0);
		dstPort.readFields(arg0);
	}

	@Override
	public void write(DataOutput arg0) throws IOException {
		// TODO Auto-generated method stub
		src.write(arg0);
		dst.write(arg0);
		pro.write(arg0);
		srcPort.write(arg0);
		dstPort.write(arg0);
	}

	@Override
	public int compareTo(FlowWritable o) {
		// TODO Auto-generated method stub
		if (src.compareTo(o.getSrc()) != 0) {
			return src.compareTo(o.getSrc());
		} else if (dst.compareTo(o.getDst()) != 0){
			return dst.compareTo(o.getDst());
		} else if (pro.compareTo(o.getPro()) != 0) {
			return pro.compareTo(o.getPro());
		} else if (srcPort.compareTo(o.getSrcPort()) != 0) {
			return srcPort.compareTo(o.getSrcPort());
		}
		return dstPort.compareTo(o.getDstPort());
 	}

	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return src.hashCode() * 52 
				+ dst.hashCode() * 42
				+ pro.hashCode() * 32
				+ srcPort.hashCode() * 22
				+ dstPort.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if (obj instanceof FlowWritable) {
			FlowWritable p1 = (FlowWritable) obj;
			return (src.equals(p1.src)) 
					&& (dst.equals(p1.dst))
					&& (pro.equals(p1.pro))
					&& (srcPort.equals(p1.srcPort))
					&& (dstPort.equals(p1.dstPort));
					
		}
		
		return false;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "[" + src.toString() + "~" 
				+ dst.toString() + "~"
				+ pro.toString() + "~" 
				+ srcPort.toString() + "~"
				+ dstPort.toString() + "]";
	}
	
	

}
